//
//  ZFBMineController.m
//  支付宝
//
//  Created by HaoYoson on 16/6/27.
//  Copyright © 2016年 HaoYoson. All rights reserved.
//

#import "NSArray+Addition.h"
#import "UIColor+Addition.h"
#import "ZFBMineController.h"
#import "ZFBMineFlowLayout.h"
#import "ZFBMineOptionCell.h"

#define kFirstSectionOptionCount 4
#define kSecondSectionOptionCount 4
#define kThirdSectionOptionCount 6

static NSString *mine_info_cell_id = @"mine_info_cell";
static NSString *mine_option_cell_id = @"mine_option_cell";
static NSString *mine_balance_cell_id = @"mine_balance_cell";

@interface ZFBMineController () <UICollectionViewDelegateFlowLayout>

@property (strong, nonatomic) NSArray *mineOptionData;

@end

@implementation ZFBMineController

- (instancetype)init {
    ZFBMineFlowLayout *layout = [[ZFBMineFlowLayout alloc] init];
    return [super initWithCollectionViewLayout:layout];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.navigationItem.title = @"我";

    // 注册单元格
    [self.collectionView registerNib:[UINib nibWithNibName:@"ZFBMineInfoCell" bundle:nil] forCellWithReuseIdentifier:mine_info_cell_id];
    [self.collectionView registerNib:[UINib nibWithNibName:@"ZFBMineOptionCell" bundle:nil] forCellWithReuseIdentifier:mine_option_cell_id];
    [self.collectionView registerNib:[UINib nibWithNibName:@"ZFBMineBalanceCell" bundle:nil] forCellWithReuseIdentifier:mine_balance_cell_id];

    self.collectionView.backgroundColor = [UIColor colorWithHex:0xf5f5f9];

    // 取消滚动条
    self.collectionView.showsVerticalScrollIndicator = NO;

    // 数据
    self.mineOptionData = [self loadMineOptionData];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 4;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    switch (section) {
        case 0:
            return 1;
            break;
        case 1:
            return kFirstSectionOptionCount + 1;
            break;
        case 2:
            return kSecondSectionOptionCount;
            break;
        case 3:
            return kThirdSectionOptionCount;
            break;

        default:
            break;
    }
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return [collectionView dequeueReusableCellWithReuseIdentifier:mine_info_cell_id forIndexPath:indexPath];
    }
    if (indexPath.section == 1 && indexPath.item == 0) {
        return [collectionView dequeueReusableCellWithReuseIdentifier:mine_balance_cell_id forIndexPath:indexPath];
    }

    ZFBMineOptionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:mine_option_cell_id forIndexPath:indexPath];
    if (indexPath.section == 1 && indexPath.item != 0) {
        cell.mineOption = self.mineOptionData[indexPath.item - 1];

    }

    else if (indexPath.section == 2) {
        cell.mineOption = self.mineOptionData[indexPath.item + kFirstSectionOptionCount];

    } else {
        cell.mineOption = self.mineOptionData[indexPath.item + kFirstSectionOptionCount + kSecondSectionOptionCount];
    }

    return cell;
}

//  cell大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return CGSizeMake(self.collectionView.bounds.size.width, 100);
    }

    if (indexPath.section == 1 && indexPath.item == 0) {
        return CGSizeMake(self.collectionView.bounds.size.width, 64);
    }

    // 每行显示的格子数量
    NSInteger count = 2;
    CGFloat w = (self.collectionView.bounds.size.width - (count - 1)) / count;
    CGFloat h = 64;
    return CGSizeMake(w, h);
}

- (NSArray *)loadMineOptionData {
    return [NSArray objectListWithPlistName:@"MineOption.plist" clsName:@"ZFBMineOption"];
}

@end
